package com.scott.threadlocal;

import java.lang.ref.WeakReference;

/**
 * @author Scott 2017/11/10 11:48
 */
public class WeakReferenceDemo {

    public static void main(String[] args) {
        Person person = new Person("Scott", 18);

        WeakReference<Person> wr = new WeakReference<>(person);

        while (true){
//            System.out.println(person); //放开此部分意味着person对象一直被引用，从而导致GC永远不会回收掉
            if(wr.get()!=null){
                System.out.println("对象依然存在" + wr);
            }else {
                System.out.println("对象被回收掉");
                break;
            }
        }
    }
}
